package com.farewell.hotel.controller;

import com.farewell.hotel.entity.InfoSearch;
import com.farewell.hotel.pojo.OneMenu;
import com.farewell.hotel.pojo.User;
import com.farewell.hotel.service.OneMenuService;
import com.farewell.hotel.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @author Farewell
 * @date 2021/4/26
 */
@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private OneMenuService oneMenuService;

    @RequestMapping("getSystemUserByLimit.do")
    public String findSystemUserByLimit(InfoSearch userInfoSearch, @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum,
                                       HttpSession httpSession) {
        if (userInfoSearch.getConditionType() == null || userInfoSearch.getKeyword() == null) {
            userInfoSearch = null;
        }
        if (userInfoSearch != null && (userInfoSearch.getConditionType().isEmpty() || userInfoSearch.getKeyword().isEmpty())) {
            userInfoSearch = null;
        }
        PageHelper.startPage(pageNum, 10);
        List<User> userList = userService.findSystemUserByLimit(userInfoSearch);
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        httpSession.setAttribute("pageInfo", pageInfo);
        return "/user/showSystemUser.jsp";
    }

    @RequestMapping("updateSystemUserStatus.do")
    public String updateSystemUserStatus(@RequestParam("userId") Long userId, @RequestParam("flag") Boolean flag,
                                         HttpServletRequest request, HttpServletResponse response) {
        try {
            userService.updateSystemUserStatus(userId, flag);
            response.sendRedirect(request.getContextPath() + "/getSystemUserByLimit.do");
        } catch (IOException e) {
            e.printStackTrace();
            return "更新失败,请重试";
        }
        return null;
    }

    @RequestMapping("toAddUser.do")
    public String toAddSystemUser(HttpSession httpSession) {
        List<OneMenu> oneMenuList = oneMenuService.findOneMenuByUserId(1L);
        httpSession.setAttribute("oneMenuList", oneMenuList);
        return "user/addSystemUser.jsp";
    }

    @RequestMapping("addSystemUser.do")
    public String addSystemUser(User user, HttpServletRequest request, HttpServletResponse response) {
        try {
            userService.addSystemUser(user);
            response.sendRedirect(request.getContextPath() + "/getSystemUserByLimit.do");
        } catch (Exception e) {
            e.printStackTrace();
            return "添加失败,请重新提交";
        }
        return null;
    }

    @RequestMapping("deleteSystemUser.do")
    public String deleteSystemUser(@RequestParam("userId")Long userId, HttpServletRequest request, HttpServletResponse response) {
        try {
            userService.deleteSystemUser(userId);
            response.sendRedirect(request.getContextPath() + "/getSystemUserByLimit.do");
        } catch (Exception e) {
            e.printStackTrace();
            return "删除失败,请重新尝试";
        }
        return null;
    }

}
